package com.company;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016.7.29.
 */
public class Calculate {

    private Calculate(){

    }

    //输入两个词，判断两个词的最大近似度：（需要给出新的书和表)
    public static double distance(String word1,String word2,
                                  Trie trie,Select select){
        ArrayList<String> arrayList1;
        ArrayList<String> arrayList2;
        try {
            arrayList1=select.getID(word1);
            arrayList2=select.getID(word2);
        }catch (SQLException ex){
            ex.printStackTrace();
            return 0;
        }

        double[] factor=new double[4];
        factor[0]=0.65;
        factor[1]=0.8;
        factor[2]=0.9;
        factor[3]=0.96;

        double maxGrade=-1;

        //循环运算，找出最大的grade
        for (String anArrayList1 : arrayList1) {
            for (String anArrayList2 : arrayList2) {
                double grade = -1;
                //组件拆分1
                String[] module1 = new String[6];
                module1[0] =anArrayList1.substring(0, 1);
                module1[1] =anArrayList1.substring(1, 2);
                module1[2] =anArrayList1.substring(2, 4);
                module1[3] =anArrayList1.substring(4, 5);
                module1[4] =anArrayList1.substring(5, 7);
                module1[5] =anArrayList1.substring(7, 8);

                //组件拆分2
                String[] module2 = new String[6];
                module2[0] = anArrayList2.substring(0, 1);
                module2[1] = anArrayList2.substring(1, 2);
                module2[2] = anArrayList2.substring(2, 4);
                module2[3] = anArrayList2.substring(4, 5);
                module2[4] = anArrayList2.substring(5, 7);
                module2[5] = anArrayList2.substring(7, 8);

                if (!module1[0].equals(module2[0])) {
                    grade = 0.1;
                } else {
                    int z = 0;
                    for (int k = 1; k < 5; k++) {
                        if (!module1[k].equals(module2[k])) {
                            String str = module1[0];
                            for (int l = 1; l < k; l++) {
                                str = str + module1[l];
                            }
                            int n = trie.getNodeNumber(str);

                            int g;
                            if (module1[k].length() == 1) {
                                g = Math.abs(module1[k].charAt(0) - module2[k].charAt(0));
                            } else {
                                g = Math.abs((module1[k].charAt(0) - module2[k].charAt(0)) * 10 +
                                        Math.abs(module1[k].charAt(1) - module2[k].charAt(1)));
                            }

                            grade = calculate(n, g, factor[k - 1]);
                            /*System.out.println(grade+" "+arrayList1.get(i)+" "+arrayList2.get(j)+" "
                                    +g+" "+n+" "+factor[k-1]);*/
                            z = 1;
                            break;
                        }
                    }
                    if (z == 0) {
                        if (module1[5].equals("@") || module2[5].equals("=")||word1.equals(word2))
                            grade = 1;
                        else
                            grade = 0.5;
                    }
                }
                if (grade > maxGrade) {
                    maxGrade = grade;
                }
            }
        }
        return maxGrade;
    }

    //输入两个词相应的参数，计算出其距离：
    public static double calculate(int n,int k,double factor){
        return factor*Math.cos(n*Math.PI/180)*(1.0*(n-k+1)/n);
    }

    //快速计算两个词是否在同一个原子词群上：
    public static boolean isSynonymy(String word1,String word2,Select select){
        ArrayList<String> arrayList1;
        ArrayList<String> arrayList2;

        try {
            arrayList1=select.getID(word1);
            arrayList2=select.getID(word2);
        }catch (SQLException ex){
            ex.printStackTrace();
            return false;
        }

        for (String anArrayList1 : arrayList1) {
            for (String anArrayList2 : arrayList2) {
                if (anArrayList1.equals(anArrayList2)
                        && anArrayList1.charAt(7) != '#') {
                    return true;
                }
            }
        }

        if(word1.equals(word2)){
            return true;
        }

        return false;
    }
}
